package flib;

import java.util.Collections;
import java.util.Vector;

import processing.core.PVector;
import simpleGeom.HasPos2D;
import simpleGeom.Vec3D;

public class FlibHENode extends Vec3D {
	public int id = -1;

	// public HEdge hEdge;

	public Object data;
	public boolean fix;
	public boolean border;
	public boolean checked;
	public int levelId;
	public int buildingId = -1;
	Vector<FlibHEdge> hEdges = new Vector<FlibHEdge>();

	public void emptyHEdges() {
		hEdges.removeAllElements();
	}

	public void connect() {
		for (int i = 0; i < hEdges.size(); i++) {
			FlibHEdge hE1 = hEdges.get(i);
			FlibHEdge hE2 = hEdges.get((i + 1) % hEdges.size());
			// hE1.next=hE2.getSym();
			hE2.getSym().setNext(hE1);
		}
	}

	public void detachHE(FlibHEdge hEdge) {
		hEdge.setNext(hEdge.getSym());
		hEdges.remove(hEdge);
		connect();
	}

	public void attachHE(FlibHEdge hEdge) {
		hEdges.add(hEdge);
		Collections.sort(hEdges, new HEdgeAngleComparator());
		connect();
	}

	public FlibHENode() {
		super();
		// TODO Auto-generated constructor stub
	}

	public FlibHENode(float x, float y, float z) {
		super(x, y, z);
		// TODO Auto-generated constructor stub
	}

	public FlibHENode(float x, float y) {
		super(x, y);
		// TODO Auto-generated constructor stub
	}

	public FlibHENode(float x, float y, FlibGraph group) {
		super(x, y);
		group.nodes.add(this);
		// TODO Auto-generated constructor stub
	}

	public FlibHENode(HasPos2D p) {
		super(p.getX(), p.getY());
		// TODO Auto-generated constructor stub
	}

	public boolean isNb(FlibHENode n2) {
		for (int i = 0; i < hEdges.size(); i++) {
			FlibHEdge hEdge = hEdges.get(i);
			if (hEdge.getDest().equalPosition(n2)) {
				return true;
			}
		}
		return false;
		/*
		 * HEdge nHEdge = hEdge; if (nHEdge==null)return false; do { if
		 * (nHEdge.getDest().equalPosition(n2)){ return true; } nHEdge =
		 * nHEdge.getNextO(); } while (!nHEdge.equals(hEdge)); return false;
		 */
	}

	public FlibHEdge getHEdge(FlibHENode n2) {
		for (int i = 0; i < hEdges.size(); i++) {
			FlibHEdge hEdge = hEdges.get(i);
			if (hEdge.getDest() == n2) {
				return hEdge;
			}
		}
		return null;
	}

	public Vector<FlibHENode> getNbs() {
		Vector<FlibHENode> nbs = new Vector<FlibHENode>();
		for (int i = 0; i < hEdges.size(); i++) {
			FlibHEdge hEdge = hEdges.get(i);
			nbs.add(hEdge.getDest());

		}
		/*
		 * HEdge nHEdge=hEdge; if (hEdge==null)return nbs; do{ HENode
		 * nb=nHEdge.getDest(); nbs.add(nb); nHEdge = nHEdge.getNextO(); }
		 * while(!nHEdge.equals(hEdge));
		 */
		return nbs;
	}

	public int getNHEdges() {
		return hEdges.size();
		/*
		 * int n=0; HEdge nHEdge=hEdge; if (hEdge==null)return 0; do{ n++;
		 * nHEdge = nHEdge.getNextO(); } while(nHEdge!=hEdge); return n;
		 */
	}

	public Vector<FlibHEdge> getHEdges() {
		return hEdges;
		/*
		 * Vector<HEdge>edges=new Vector<HEdge>(); HEdge nHEdge=hEdge; if
		 * (hEdge==null)return edges; do{ edges.add(nHEdge); nHEdge =
		 * nHEdge.getNextO(); } while(!nHEdge.equals(hEdge)); return edges;
		 */
	}

	public FlibHEdge getHEdge() {
		if (hEdges.size() == 0)
			return null;
		return hEdges.firstElement();
		// return hEdge;
	}

	public void setHEdge(FlibHEdge edge) {
		// hEdge = edge;

	}

	public Object getData() {
		// TODO Auto-generated method stub
		return data;
	}

	public void setData(Object data) {
		// TODO Auto-generated method stub
		this.data = data;
	}

	public boolean isChecked() {
		return checked;
	}

	public void setChecked(boolean checked) {
		this.checked = checked;
	}

	public FlibHENode clone() {
		FlibHENode node = new FlibHENode(x, y, z);
		node.buildingId = this.buildingId;
		node.levelId = this.levelId;
		node.id = this.id;
		return node;
	}

}
